package org.kk.linked;

import org.kk.Common;

/**
 * 反转链表
 * @author kk
 * @Date 2021/1/23
 */
public class ReverseList {
    /**
     * 解法一：迭代：两个指针，反向输出
     * 时间复杂度：O(n)，空间复杂度：O(1)
     * 示例
     * 初始化: head = [1->2->3->4->5]
     * 反转后: head = [5->4->3->2->1]
     */
    public static Common.ListNode reverseList1(Common.ListNode head) {
        Common.ListNode pre = null;
        Common.ListNode curr = head;

        while (curr != null) {
            Common.ListNode tmp = curr.next;
            curr.next = pre;
            pre = curr;
            curr = tmp;
        }
        return pre;
    }

}
